package com.fang.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.fang.exception.UserNotExistException;

/**
 * 异常处理拦截类
 */
@ControllerAdvice
public class ControllerExceptionHandler {

	public ControllerExceptionHandler() {
		// TODO Auto-generated constructor stub
	}

	// 一旦发现存在UserNotExistException这个异常，就会到这里来处理,同时这个异常会处理掉跑到这个类中的异常即在拦截器中个不在获取到
	@ResponseBody
	@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(UserNotExistException.class)
	public Map<String, Object> handleUserNotExistException(UserNotExistException exception) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", exception.getId());
		map.put("message", exception.getMessage());
		return map;
	}

}
